// Created by carolsail

import { ENUM_AUDIO_CLIP, ENUM_UI_TYPE } from "../Enum";
import { StaticInstance } from './../StaticInstance';
import AudioManager from "../manager/AudioManager";
import BaseLayer from "./Baselayer";
import DataManager from "../manager/DataManager";
import PoolManager from "../manager/PoolManager";
import ResourceManager from "../manager/ResourceManager";
import SdkManager from "../manager/SdkManager";
import ToastManager from "../manager/ToastManager";
import { random } from "../Utils";
import { privacyChannel } from "../Script/privacyChannel";

const { ccclass, property } = cc._decorator;

@ccclass
export default class SkinLayer extends BaseLayer {

    panel: cc.Node = null
    btnUse: cc.Node = null
    btnCancel: cc.Node = null
    skinIcon: cc.Node = null
    skinIndex: number = 1

    onLoad() {
        this.panel = cc.find('style/panel', this.node)
        this.btnUse = cc.find('buttons/btn_use', this.panel)
        this.btnCancel = cc.find('buttons/btn_cancel', this.panel)
        this.skinIcon = this.panel.getChildByName('icon')
        this.btnUse.on('click', this.onUseClick, this)
        this.btnCancel.on('click', this.onCancelClick, this)
    }

    onDestroy() {
        this.btnUse.off('click', this.onUseClick, this)
        this.btnCancel.off('click', this.onCancelClick, this)
    }

    onEnable() {
        this.zoomIn(this.panel)
        this.rendor()
        SdkManager.instance.toggleCustomRowAd(true)
    }

    onDisable() {
        SdkManager.instance.toggleCustomRowAd(false)
    }
    show(): void {
        super.show()
        switch (privacyChannel.channel) {
            case "oppo":
            case "oppo1":
            case "vivo":
            case "vivo1":
            case "xm":
                if (privacyChannel.getChannel().getCtrl("p2")) {

                    privacyChannel.getChannel().showInter(true)
                    try {
                        privacyChannel.getChannel().showBanner()
                    } catch {

                    }
                }
                break
            case "ks":
                if (privacyChannel.getChannel().getCtrl("p2")) {

                    privacyChannel.getChannel().showInter(true)
                }
                break
        }
    }
    rendor() {
        const skinIndexArr = []
        for (let i = 0; i < DataManager.instance.skinTotal; i++) {
            const skinIndex = i + 1
            if (!DataManager.instance.hasSkinIndex(skinIndex)) skinIndexArr.push(skinIndex)
        }
        this.skinIndex = skinIndexArr[random(0, skinIndexArr.length - 1)]
        this.skinIcon.getComponent(cc.Sprite).spriteFrame = ResourceManager.instance.getSprite(`bottle${this.skinIndex}`)
        // 解锁
        DataManager.instance.addSkinIndex(this.skinIndex)
        StaticInstance.uiManager.setSkinReset()
    }

    onUseClick() {
        AudioManager.instance.playSound(ENUM_AUDIO_CLIP.CLICK)
        DataManager.instance.skinIndex = this.skinIndex
        DataManager.instance.save()
        StaticInstance.uiManager.setSkinReset()
        StaticInstance.uiManager.toggle(ENUM_UI_TYPE.SKIN_GET, false)
    }

    onCancelClick() {
        AudioManager.instance.playSound(ENUM_AUDIO_CLIP.CLICK)
        StaticInstance.uiManager.toggle(ENUM_UI_TYPE.SKIN_GET, false)
    }
}
